package com.dirox.plugins.jira;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.plugin.profile.ViewProfilePanel;
import com.atlassian.jira.plugin.profile.ViewProfilePanelModuleDescriptor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.plugin.webresource.WebResourceManager;
import com.dirox.plugins.ao.entities.Note;
import com.dirox.plugins.ao.service.NoteService;

public class PostItsBoard implements ViewProfilePanel {
	private final JiraAuthenticationContext context;
	private ViewProfilePanelModuleDescriptor moduleDescriptor;

	private NoteService noteService;
	private UserManager userManager;
	private IssueService issueService;
	private SearchService searchService;
	private ProjectManager projectManager;
	private WebResourceManager webResourceManager;

	public PostItsBoard(JiraAuthenticationContext context,
			NoteService noteService, UserManager userManager,
			SearchService searchService, IssueService issueService,
			ProjectManager projectManager,
			WebResourceManager webResourceManager) {
		super();
		this.context = context;
		this.noteService = checkNotNull(noteService);
		this.userManager = checkNotNull(userManager);
		this.searchService = checkNotNull(searchService);
		this.issueService = checkNotNull(issueService);
		this.projectManager = checkNotNull(projectManager);
		this.webResourceManager = checkNotNull(webResourceManager);
	}

	@Override
	public String getHtml(User profileUser) {
		webResourceManager.requireResource("com.dirox.plugins.jira.Notes:note-res");
		return moduleDescriptor.getHtml(VIEW_TEMPLATE, getParams(profileUser));
	}

	@Override
	public void init(ViewProfilePanelModuleDescriptor descriptor) {
		this.moduleDescriptor = descriptor;
	}

	public List<Issue> getIssues(String projectName) {
		final User currentUser = context.getLoggedInUser();
		JqlClauseBuilder jqlClauseBuilder = JqlQueryBuilder.newClauseBuilder();
		com.atlassian.query.Query query = jqlClauseBuilder.project(projectName)
				.buildQuery();
		PagerFilter pagerFilter = PagerFilter.getUnlimitedFilter();
		com.atlassian.jira.issue.search.SearchResults searchResults = null;
		try {
			searchResults = searchService.search(currentUser, query,
					pagerFilter);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		return searchResults.getIssues();
	}

	public List<Issue> getIssues() {
		List<Issue> issues = new ArrayList<Issue>();
		List<Project> projects = projectManager.getProjectObjects();
		for (Project p : projects) {
			issues.addAll(getIssues(p.getName()));
		}
		return issues;
	}

	private Map<String, Object> getParams(User profileUser) {
		final User currentUser = context.getLoggedInUser();
		final Map<String, Object> params = new HashMap<String, Object>();

		List<User> users = new ArrayList<User>();
		users.addAll(userManager.getAllUsers());
		users.remove(currentUser);
		
		List<Note> notes = noteService.getNotes(-1);
		List<Note> memos = noteService.getMemos();
		List<Issue> issues = getIssues();
		List<Project> projects = projectManager.getProjectObjects();
		
		params.put("notes", notes);
		params.put("memos", memos);
		params.put("users", users);
		params.put("issues", issues);
		params.put("projects", projects);
		
		return params;
	}
}
